package com.pwl.springBoot.modules.test.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pwl.springBoot.modules.test.service.CityService;
import com.pwl.springBoot.modules.common.vo.Result;
import com.pwl.springBoot.modules.common.vo.Search;
import com.pwl.springBoot.modules.test.dao.CityDao;
import com.pwl.springBoot.modules.test.entity.City;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * City Service Impl
 */
@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    @Override
    @Transactional
    public Result<City> insertCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if (temp != null){
            return new Result<>(Result.ResultStatus.FAILED.code,"城市名重复");
        }

        city.setDateModified(new Date());
        city.setDateCreated(new Date());
        cityDao.insertCity(city);

        return new Result<>(Result.ResultStatus.SUCCESS.code,"插入成功",city);
    }

    @Override
    @Transactional
    public Result<City> updateCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if (temp != null && temp.getCityId() != city.getCityId()){
            return new Result<>(Result.ResultStatus.FAILED.code,"城市名重复");
        }

        city.setDateModified(new Date());

        cityDao.updateCity(city);
        return new Result<>(Result.ResultStatus.SUCCESS.code,"更新成功",city);
    }

    @Override
    @Transactional
    public Result<City> deleteCity(int cityId) {
        cityDao.deleteCityByCityId(cityId);
        return new Result<>(Result.ResultStatus.SUCCESS.code,"删除成功");
    }

    @Override
    public City getCityByCityId(int cityId) {
        return cityDao.getCityByCityId(cityId);
    }

    @Override
    public List<City> getCitiesByCountryId(int countryId) {
        return Optional
                .ofNullable(cityDao.getCitiesByCountryId(countryId))
                .orElse(Collections.emptyList());
    }

    @Override
    public PageInfo<City> getCitiesBySearch(Search search) {
        search.init();
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(cityDao.getCitiesBySearch(search))
                .orElse(Collections.emptyList()));
    }

    @Override
    @Transactional
    public Result<Object> insertBatchCities(@Param("cities") List<City> cities) {
        cities.stream().forEach(item ->{
            item.setDateModified(new Date());
            item.setDateCreated(new Date());
        });

        cityDao.insertBatchCities(cities);
        return new Result<>(Result.ResultStatus.SUCCESS.code,"批量插入成功！");
    }
}
